//
//  GQInfomationHeaderView.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/16.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQInfomationHeaderView.h"
#import "GQInfoHeaderViewCell.h"
#define flag @"flag"

@interface GQInfomationHeaderView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>

@property (weak, nonatomic) UICollectionView *collectionView;
@property (assign, nonatomic) NSInteger count;
@property (weak ,nonatomic) NSTimer *myTimer;

@end

@implementation GQInfomationHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpChildView];
        [self addNSTimer];
    }
    return self;
}


- (void)automaticPlay
{
    NSIndexPath *currentIndexiPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
    NSInteger nextItem = currentIndexiPath.item + 1;
    NSInteger nextSection = currentIndexiPath.section;
    if (nextItem == 3) {
        nextItem = 0;
        nextSection = 0;
    }
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

- (void)setUpChildView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(self.width, self.height);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:layout];
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.pagingEnabled = YES;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.bounces = NO;
    [collectionView registerClass:[GQInfoHeaderViewCell class] forCellWithReuseIdentifier:flag];
    [self addSubview:collectionView];
    self.collectionView = collectionView;
}

- (void)addNSTimer
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(automaticPlay) userInfo:nil repeats:YES];
    self.myTimer =  timer;
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)removeNSTimer
{
    [self.myTimer invalidate];
    self.myTimer = nil;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeNSTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addNSTimer];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GQInfoHeaderViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:flag forIndexPath:indexPath];
    cell.image = [UIImage imageNamed:@"icon"];
    cell.titleText = @"  藏友将私藏的300余件铜镜博物馆";
    cell.pageText = [NSString stringWithFormat:@"%ld/3", indexPath.row + 1];
    return cell;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}




@end
